/***************************************************************
 * FILENAME:  usart3200.c
 * AUTHOR:    zimmermane@msoe.edu <Evan Zimmerman>
 *			  duerk@msoe.edu <Kevin Duer>
 * DATE:      11 Sept 2012
 * PROVIDES:  
 *    - USART Device Driver 
 *    - Contains subroutines and functions for the USART
 *   
 * ucsra: RXC TXC UDRE FE DOR PE U2X MPCM
 *
 * ucsrb: RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8
 **************************************************************/
#include "usart3200.h"
#include <avr/io.h>

/*
 * usart_init
 * Purpose: initialize the usart
 *			default: 9600 baud , 1 stop, no parity, transmitter on
 *			 ubrr = f_cpu/(baud*16) -1
 * Parameters: None
 * Returns: None
 */
void usart_init(void){
   UBRRH = 0;
   UBRRL = 0x67;
   UCSRB = (1<<TXEN) | (1<<RXEN) | (1<<RXCIE); // enable transmitter/receiver
								  //recieve interrupt
}

/*
 * usart_baud
 * Purpose: allows the user to set the desired baud rate
 * Parameters: highByte: byte to be written to ubrrh
 *			   lowByte: byte to be written to ubrrl
 * Returns: None
 */
void usart_baud(unsigned char highByte, unsigned char lowByte){
   UBRRH = highByte; 
   UBRRL = lowByte;
}

/*
 * usart_send_data
 * Purpose: sends a byte to the udr to be transmitted
 * Parameters: theData: byte to be sent to the UDR
 * Returns: None
 */
void usart_send_data(unsigned char theData){
   while(!(UCSRA & (1<<UDRE))); // while UDRE is 0, trap here
   UDR = theData; 

}

